/**
 * Created by George on 2017/8/6.
 */
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";

    $scope.activityRecordDatas = [];
    $scope.memberListDatas = [];
    $scope.duesRecordDatas = [];
    $scope.recharge = {
        userId: "",
        dues: 100,
        date: getNowFormatDate(),
        info: "会费充值",
        usersData: []
    };
    $scope.user = {
        name: "",
        dues: 0,
        info: "",
        join_date: getNowFormatDate()
    };
    $scope.activity = {
        name: "",
        date: getNowFormatDate(),
        cost: 0,
        info: "",
        selectUsers: [],
        usersData: [],
        type: "0",
        typeData: [
            {id: "0", name: "支出"},
            {id: "1", name: "收入"}
        ],
        number: 0
    };
    $scope.dues = {
        consume: 0,
        realConsume: 0,
        leave: 0
    };

    $scope.editUser = {
        id: "",
        name: "",
        info: "",
        join_date: getNowFormatDate()
    };

    $scope.addActivityClick = function () {
        $scope.activity.selectUsers = [];
        for (var index = 0; index < $scope.activity.usersData.length; index++) {
            if ($scope.activity.usersData[index].checked === true && $scope.activity.usersData[index].number > 0) {
                var user = {
                    id: $scope.activity.usersData[index].id,
                    number: $scope.activity.usersData[index].number,
                    name: $scope.activity.usersData[index].name
                };
                $scope.activity.selectUsers.push(user);
            }
        }

        if ($scope.activity.selectUsers.length <= 0) {
            console.log("must select ");
            alert("必须选择人员");
            return;
        }

        if ($scope.activity.cost < 0) {
            alert("金额必须大于零");
            return;
        }

        var data = {
            "name": $scope.activity.name,
            "type": $scope.activity.type,
            "date": $scope.activity.date,
            "cost": $scope.activity.cost,
            "users": $scope.activity.selectUsers,
            "info": $scope.activity.info
        };
        $http.post('/basketballfire/addActivity', data).then(function () {
            console.log("success!");
            alert("创建成功！");
            initOrUpdateData();
        }, function () {
            alert("创建失败！");
            console.log("failed!");
        });
    };

    $scope.addMemberClick = function () {
        var data = {
            "name": $scope.user.name,
            "dues": $scope.user.dues,
            "info": $scope.user.info,
            "join_date": $scope.user.join_date
        };
        $http.post('/basketballfire/user', data).then(function (req) {
            console.log("success!");
            alert("创建成功！");
            initOrUpdateData();
        }, function (req) {
            console.log("failed!");
            alert("创建失败！");
        });
    };

    $scope.editMemberClick = function () {
        var data = {
            "id": $scope.editUser.id,
            "name": $scope.editUser.name,
            "info": $scope.editUser.info,
            "join_date": $scope.editUser.join_date
        };
        $http.put('/basketballfire/user', data).then(function (req) {
            console.log("success!");
            alert("更新成功！");
            initOrUpdateData();
        }, function (req) {
            console.log("failed!");
            alert("更新失败！");
        });
    };

    $scope.addRechargeClick = function () {
        // $scope.recharge.userId = $('#rechargeUsersSelect option:selected').val();
        var data = {
            "userId": $scope.recharge.userId,
            "dues": $scope.recharge.dues,
            "date": $scope.recharge.date,
            "info": $scope.recharge.info
        };
        $http.post('/basketballfire/addRecharge', data).then(function () {
            console.log("success!");
            alert("创建成功！");
            initOrUpdateData();
        }, function () {
            console.log("failed!");
            alert("创建失败！");
        });
    };

    $scope.selectUserChange = function () {
        var number = 0;
        $scope.activity.selectUsers = [];
        for (var index = 0; index < $scope.activity.usersData.length; index++) {
            if ($scope.activity.usersData[index].checked === true && $scope.activity.usersData[index].number > 0) {
                var user = {
                    id: $scope.activity.usersData[index].id,
                    number: $scope.activity.usersData[index].number,
                    name: $scope.activity.usersData[index].name
                };
                number += $scope.activity.usersData[index].number;
                $scope.activity.selectUsers.push(user);
            }
        }
        $scope.activity.number = number;
    };

    //程序入口，初始化
    init();

    function init() {
        initOrUpdateData();
    }

    function initUsersData(usersData) {
        $scope.memberListDatas = usersData;
        $scope.recharge.usersData = [];
        $scope.activity.usersData = [];
        $scope.dues.leave = 0;
        for (var index = 0; index < usersData.length; index++) {
            var user0 = {
                "id": usersData[index].id,
                "name": usersData[index].name
            };
            $scope.recharge.usersData.push(user0);

            var user1 = {
                "id": usersData[index].id,
                "name": usersData[index].name,
                "checked": false,
                "number": 1
            };
            $scope.activity.usersData.push(user1);

            $scope.dues.leave = add($scope.dues.leave, usersData[index].dues);
        }
    }

    function initOrUpdateData() {
        $scope.recharge = {
            userId: "",
            dues: 100,
            date: getNowFormatDate(),
            info: "会费充值",
            usersData: []
        };
        $scope.user = {
            name: "",
            dues: 0,
            info: "",
            join_date: getNowFormatDate()
        };
        $scope.activity = {
            name: "篮球活动",
            date: getNowFormatDate(),
            cost: 0,
            info: "租场费、水费",
            selectUsers: [],
            usersData: [],
            type: "0",
            typeData: [
                {id: "0", name: "支出"},
                {id: "1", name: "收入"}
            ],
            number: 0
        };
        $scope.editUser = {
            id: "",
            name: "",
            info: "",
            join_date: getNowFormatDate()
        };

        $http.get("/basketballfire/memberList").then(function (data, status) {
            console.log(data);
            var usersData = data.data;
            initUsersData(usersData);
        });

        $http.get("/basketballfire/duesRecord").then(function (data, status) {
            console.log(data);
            $scope.duesRecordDatas = data.data;
        });

        $http.get("/basketballfire/activityRecord").then(function (data, status) {
            console.log(data);
            $scope.activityRecordDatas = data.data;
            $scope.dues.consume = 0;
            for (var index = 0; index < $scope.activityRecordDatas.length; index++) {
                var activityRecord = $scope.activityRecordDatas[index];
                var usersDesc = "";
                for (var i = 0; i < activityRecord.users.length; i++) {
                    if (activityRecord.users[i].number > 1) {
                        usersDesc += " ;" + activityRecord.users[i].name + " x " + activityRecord.users[i].number;
                    } else {
                        usersDesc += " ;" + activityRecord.users[i].name;
                    }
                }
                usersDesc = usersDesc.substr(2);
                activityRecord.usersDesc = usersDesc;
                $scope.dues.consume += $scope.activityRecordDatas[index].cost;
            }
        });
    }

    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        return currentdate;
    }

    function add(a, b) {
        var c, d, e;
        try {
            c = a.toString().split(".")[1].length;
        } catch (f) {
            c = 0;
        }
        try {
            d = b.toString().split(".")[1].length;
        } catch (f) {
            d = 0;
        }
        return e = Math.pow(10, Math.max(c, d)), (mul(a, e) + mul(b, e)) / e;
    }

    function mul(a, b) {
        var c = 0,
            d = a.toString(),
            e = b.toString();
        try {
            c += d.split(".")[1].length;
        } catch (f) {
        }
        try {
            c += e.split(".")[1].length;
        } catch (f) {
        }
        return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
    }
})
;